define(function(require, exports, module) {
	/**
	 * config={
	 * 		parentId:'父容器id',
	 * 		fileName:'',
	 * 		uploadFileURL:'',
	 * };
	 * */
	var UploadFile=function(config){
		this.parentId='';
		this.id='';
		this.fileName='';
		this.showDownLink=true;
		this.uploadFileURL='';
		this.downloadFileURL='';
		this.accept='';
		this.editable=true;
		this.afterUploadFile=function(fileName){};
		$.extend(this,config,true);
		this.id=this.id||(this.parentId+'uploadFile');
		this.$parentId=$('#'+this.parentId);
		this.init();
	};
	UploadFile.prototype={
		init:function(){
			this.render();
			this.baseBind();
			this.editable&&this.editableBind();
		},
		render:function(){
			//增加操作的上传控件和下载链接
			var html=[],
				accept=this.accept?'accept="'+this.accept+'"':'',
				linkShow=this.fileName?'':'style="display:none"';
			html.push('<div class="col-xs-10" style="padding-left:0">');
			html.push('	<input type="input" class="form-control readbind writebind" style="cursor:pointer" readonly name="bisRequireDoc" id="'+this.id+'-bisRequireDoc">');
			html.push('</div>');
			html.push('<div class="col-xs-2" style="padding:0">');
			html.push('	<p class="form-control-static" style="cursor: pointer;" ><a '+linkShow+' class="link-download">下载</a></p>');
			html.push('</div>');
			this.$parentId.html(html.join(""));
			
			//增加隐藏的文件提交form
			html=[];
			html.push('<div style="display:none">');
			html.push('	<form action="'+this.uploadFileURL+'" name="'+this.id+'-form" id="'+this.id+'-form" encType="multipart/form-data"  method="post" target="'+this.id+'-hidden_frame" >   ');
			html.push('		<input type="file" '+accept+' class="form-control readbind writebind" name="file" id="'+this.id+'-file">');
			html.push('		<iframe style="display:none" name="'+this.id+'-hidden_frame" id="'+this.id+'-hidden_frame"></iframe>   ');
			html.push('	</form> ');
			html.push('</div>');
			$('body').append(html.join(""));
		},
		baseBind:function(){
			var that=this;
			//点击链接按钮下载文件
			this.$parentId.on('click','.link-download',function(){
				if(that.downloadFileURL)
					window.location.href=that.downloadFileURL+'?fileName='+that.fileName;
				return false;
			});
		},
		editableBind:function(){
			var that=this,
				frameId=this.id+'-hidden_frame',
				$bisRequireDoc=$('#'+this.id+'-bisRequireDoc'),
				$fileId=$('#'+this.id+'-file');
			//点击输入框时间：
			$bisRequireDoc.on('click',function(){
				$fileId.click();
				return false;
			});
			//当上传控件值变化的时候，上传文件到服务器
			$fileId.on('change',function(){
				that.beforeUploadFile&&that.beforeUploadFile();
				$('#'+that.id+'-form').submit();
			});
			//文件上传到服务器后返回 保存的文件名称
			$('#'+frameId).on('load',function(){
				that.fileName=$(window.frames[frameId].document.body).html();
				//回写文本框的文件名
				$bisRequireDoc.val(that.fileName);
				//显示下载链接
				that.showDownLink&&$('.link-download',that.$parentId).css('display','inline');
				that.afterUploadFile(that.fileName);
			});
		},
		getFileName:function(){
			return this.fileName;
		},
		setFileName:function(fileName){
			this.fileName=fileName;
		}
	};
	return UploadFile;
});
